import 'package:enjoy_plus_community/pages/house/building_list.dart';
import 'package:enjoy_plus_community/pages/house/house_detail.dart';
import 'package:enjoy_plus_community/pages/house/house_form.dart';
import 'package:enjoy_plus_community/pages/house/house_list.dart';
import 'package:enjoy_plus_community/pages/house/location_list.dart';
import 'package:enjoy_plus_community/pages/house/room_list.dart';
import 'package:enjoy_plus_community/pages/login/index.dart';
import 'package:enjoy_plus_community/pages/notice/index.dart';
import 'package:enjoy_plus_community/pages/profile/index.dart';
import 'package:enjoy_plus_community/pages/tab_bar_page.dart';
import 'package:enjoy_plus_community/store/CountModel.dart';
import 'package:enjoy_plus_community/utils/token.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(
    MultiProvider(
      // 这段代码在 Flutter 中使用 `ChangeNotifierProvider` 创建并提供一个 `CountModel` 实例，用于状态管理。
      // 解释如下：
      // - `providers: [...]`：定义一组状态提供者。
      // - `ChangeNotifierProvider`：一个专门用于提供继承自 `ChangeNotifier` 的模型类的 Provider。
      // - `create: (_) => CountModel()`：当需要创建模型实例时，调用此函数生成一个新的 `CountModel` 对象。
      providers: [ChangeNotifierProvider(create: (_) => CountModel())],
      child: MaterialApp(
        routes: {
          '/': (context) => const TabBarPage(),
          '/detail': (context) => const NoticeDetail(),
        },
        onGenerateRoute: (settings) {
          final tokenManager = TokenManager();
          final token = tokenManager.getToken() ?? '';
          if (token.isEmpty && settings.name != '/login') {
            return MaterialPageRoute(builder: (context) => const LoginPage());
          }

          if (settings.name == '/profile') {
            return MaterialPageRoute(
              builder:
                  (context) => ProfilePage(userInfo: settings.arguments as Map),
            );
          }
          if (settings.name == '/house_list') {
            return MaterialPageRoute(builder: (context) => const HouseList());
          }
          if (settings.name == '/add_house') {
            return MaterialPageRoute(
              builder: (context) => const LocationList(),
            );
          }
          if (settings.name == '/building_list') {
            print(settings.arguments);
            return MaterialPageRoute(
              builder:
                  (context) =>
                      BuildingList(point: (settings.arguments as Map)['point']),
            );
          }
          if (settings.name == '/room_list') {
            Map temp = settings.arguments as Map;
            return MaterialPageRoute(
              builder:
                  (context) => RoomList(
                    point: temp['point'],
                    building: temp['building'],
                  ),
            );
          }
          if (settings.name == '/house_form') {
            return MaterialPageRoute(
              builder:
                  (context) => HouseForm(params: settings.arguments as Map),
            );
          }
          if (settings.name == '/house_detail') {
            return MaterialPageRoute(
              builder:
                  (context) =>
                      HouseDetail(id: (settings.arguments as Map)['id']),
            );
          }
          return null;
        },
        initialRoute: '/',
      ),
    ),
  );
}
